package controllers;

import java.util.Map;

import models.cms.LayoutDto;
import models.cms.PageDto;
import models.core.user.UserDtoInfo;
import play.libs.F.Function;
import play.mvc.Controller;
import play.mvc.Result;
import services.ServiceException;
import services.cms.PageService;
import services.core.UserService;
import controllers.cms.CmsException;
import controllers.cms.pages.PageManager;

public class Application extends Controller {
  
  public static Result index() {
	  return show("home");
  }
  
  public static String getToken(){
	 if(session(Authentication.getSessionName()) != null){
		 return session(Authentication.getSessionName());
	 }
	  if(request().cookies() != null 
			  && request().cookies().get(Authentication.getSessionName()) != null){
		  return request().cookies().get(Authentication.getSessionName()).value();
	  }
	  return null;
  }
  
  public static Result show(String page) {
	  try {
		    final Map<String, String[]> params = request().queryString();
		    final String vv_session = getToken();
			return async(
				PageService.getPageByName(page).map(
					new Function<PageDto, Result>(){
						public Result apply(PageDto dto){
							if(params != null){
								dto.addParams(params);
							}
							 if(vv_session != null){
								UserDtoInfo user = UserService.getUserInfo(vv_session).get();
						    	dto.addData(PageDto.USER,user);
							} 
							return renderPage(dto);
						}
					}
				)
			);
		} catch (ServiceException e) {
			return badRequest(e.getMessage());
		}
  }
  
  
  public static Result showByContent(final String page, final String content) {
	  try {
		    final Map<String, String[]> params = request().queryString();	
		    final String vv_session = getToken();
    		
		    return async(
				PageService.getPageByContent(page, content).map(
					new Function<PageDto, Result>(){
						public Result apply(PageDto dto){
							dto.addParams(PageDto.CONTENT, content);
							 if(vv_session != null){
									UserDtoInfo user = UserService.getUserInfo(vv_session).get();
							    	dto.addData(PageDto.USER,user);
								} 
							if(params != null){
								dto.addParams(params);
							}
							return renderPage(dto);
						}
					}
				)
			);
		} catch (ServiceException e) {
			return badRequest(e.getMessage());
		}
  	}
  
  	protected static Result renderPage(PageDto dto){
	  	LayoutDto l = dto.getLayout();
		String pageManagerName = "controllers.cms.pages.PageManagerImpl";
		if(l.getController() != null){
			pageManagerName = "controllers.cms.pages." + l.getController();
		}
		try {
			Class pageManagerClass = Class.forName(pageManagerName);
			PageManager pageManager = (PageManager)pageManagerClass.newInstance();
			return ok(pageManager.render(dto));
		} catch (CmsException e) {
			return internalServerError(e.getMessage());
		} catch (ClassNotFoundException e) {
			return internalServerError(e.getMessage());
		} catch (InstantiationException e) {
			return internalServerError(e.getMessage());
		} catch (IllegalAccessException e) {
			return internalServerError(e.getMessage());
		}  
  	}
}
